package com.example.administrator.student4;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2016/12/11.
 */
public class WebUtil {
    public static void jsonOut(JSONArray array, HttpURLConnection connection) throws IOException {
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();
        if(connection.getResponseCode() == 200)connection.getInputStream();
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(String.valueOf(array));
        writer.flush();
    }
    public static void jsonOut(JSONObject object, HttpURLConnection connection) throws IOException{
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(String.valueOf(object));
        writer.flush();
        if(connection.getResponseCode() == 200) connection.getInputStream();
    }
    public static JSONArray ioToJsonArray(InputStream is) throws IOException, JSONException {
        JSONArray array = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = is.read(buffer))!=-1){
            baos.write(buffer,0,len);
        }
        String jsonString = baos.toString();
        array = new JSONArray(jsonString);
        return array;
    }
    public static JSONObject ioToJsonObject(InputStream is) throws IOException, JSONException {
        JSONObject object = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = is.read(buffer))!=-1){
            baos.write(buffer,0,len);
        }
        String jsonString = baos.toString();
        object = new JSONObject(jsonString);
        return object;
    }
    public static String[] subjectfind() throws IOException, JSONException {
        String path = "http://10.0.2.2:8080/andorid/subject?request=findsubjectname";
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        JSONArray array = ioToJsonArray(connection.getInputStream());
        String[] result = new String[array.length()];
        for(int i = 0;i<array.length();i++){
            result[i] = array.getJSONObject(i).getString("name");
        }
        return result;
    }
    public static String[] classfind() throws IOException, JSONException {
        String path = "http://10.0.2.2:8080/andorid/student?request=class";
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        JSONArray array = ioToJsonArray(connection.getInputStream());
        String[] result = new String[array.length()];
        for(int i = 0;i<array.length();i++){
            result[i] = array.getJSONObject(i).getString("class");
        }
        return result;
    }
    public static String[] namefind(String name) throws IOException, JSONException {
        String path = "http://10.0.2.2:8080/andorid/student?request=findname&name="+name;
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        JSONArray array = ioToJsonArray(connection.getInputStream());
        String[] result = new String[array.length()];
        for(int i = 0;i<array.length();i++){
            result[i] = array.getJSONObject(i).getString("name");
        }
        return result;
    }
}
